﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Drawing;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyOA.BLL;
using MyOA.Model;

namespace MyOA.UI.Message.MessageManage
{
    public partial class MessageManage : System.Web.UI.Page
    {
        MessageManager messageManager = new MessageManager();
        UserInfoManager userInfoManager = new UserInfoManager();
        MessageToUserManager messageToUserManager = new MessageToUserManager();
        string word = string.Empty;        //初始化要提示的语句
        UserInfo userInfo = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["loginId"] == null)
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                string loginId = Session["loginId"].ToString();
                userInfo = userInfoManager.GetUserInfoByLoginId(loginId); //获取用户信息
                //首次加载绑定数据信息
                if (!IsPostBack)
                {
                    LoadCountNotReadMessage();
                    dataBindReceiveMessage();
                    dataBindSendMessage();
                    dataBindRecycleMessage();
                    dataBindDraftMessage();
                }
            }
        }
        //加载时显示未读短信数目
        private void LoadCountNotReadMessage()
        {
            int sum;
            int num = Convert.ToInt32((messageManager.getAllNoReadReceiveMessageByUserId(userInfo.UserId, 5, 1, 0, out sum)).Count);
            lblNotRead.Text = "您有" + Convert.ToString(num) + "未读短消息，请查收！";
        }
        //首次加载绑定收信箱数据
        private void dataBindReceiveMessage()
        {
            int sum;
            int userId = userInfo.UserId;
            IList<MyOA.Model.Message> messageList = messageManager.GetAllReceiveMessageByUserId(userId, 5, 1, 0, out sum);
            anpReceive.RecordCount = sum;
            anpReceive.PageSize = 5;
            this.dataBindReceiveMessage(messageList);
        }
        //首次加载绑发送信箱数据
        private void dataBindSendMessage()
        {
            int sum;
            //string loginId = Session["loginId"].ToString();
            //UserInfo userInfo = userInfoManager.GetUserInfoByLoginId(loginId);
            int userId = userInfo.UserId;
            IList<MyOA.Model.Message> messageList = messageManager.GetAllSendMessageByUserId(userId, 5, 1, 0, out sum);
            anpSend.RecordCount = sum;
            anpSend.PageSize = 5;
            this.dataBindSendMessage(messageList);
        }
        //首次加载绑删除信箱数据
        private void dataBindRecycleMessage()
        {
            int sum;
            int userId = userInfo.UserId;
            IList<MyOA.Model.Message> messageList = messageManager.GetAllRecycleMessageByUserId(userId, 5, 1, 0, out sum);
            anpRecycle.RecordCount = sum;
            anpRecycle.PageSize = 5;
            this.dataBindRecycleMessage(messageList);
        }
        //首次加载绑草稿信箱数据
        private void dataBindDraftMessage()
        {
            int sum;
            int userId = userInfo.UserId;
            IList<MyOA.Model.Message> messageList = messageManager.GetAllDraftMessageByUserId(userId, 5, 1, 0, out sum);
            anpDraft.RecordCount = sum;
            this.dataBindDraftMessage(messageList);
        }

        protected string CutString(int length, object str)
        {
            if (str.ToString().Length < length - 3)
            {
                return str.ToString();
            }
            else
            {
                return str.ToString().Substring(0, length - 3) + "...";
            }
        }
        //发送新消息
        protected void btnNewMessage_Click(object sender, EventArgs e)
        {
            Response.Redirect("SendMessage.aspx");
        }
        //显示全部信息
        protected void btnShowAll_Click(object sender, EventArgs e)
        {
            dataBindReceiveMessage();
        }
        //获取未读信息
        protected void btnNotRead_Click(object sender, EventArgs e)
        {
            int sum;
            IList<MyOA.Model.Message> messageList = messageManager.getAllNoReadReceiveMessageByUserId(userInfo.UserId, 5, 1, 0, out sum);
            anpReceive.RecordCount = sum;
            this.dataBindReceiveMessage(messageList);
        }
        //获取已读信息
        protected void btnRead_Click(object sender, EventArgs e)
        {
            int sum;
            IList<MyOA.Model.Message> messageList = messageManager.getAllReadReceiveMessageByUserId(userInfo.UserId, 5, 1, 0, out sum);
            anpReceive.RecordCount = sum;
            this.dataBindReceiveMessage(messageList);
        }
        //点击已删除信箱按钮，隐藏其他GridView
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            this.div_Send.Visible = false;
            this.div_Receive.Visible = false;
            this.div_Recycle.Visible = true;
            this.div_Draft.Visible = false;
        }
        //点击发信箱按钮，隐藏其他GridView
        protected void btnSend_Click(object sender, EventArgs e)
        {
            this.div_Send.Visible = true;
            this.div_Receive.Visible = false;
            this.div_Recycle.Visible = false;
            this.div_Draft.Visible = false;
        }
        //点击收信箱按钮，隐藏其他GridView
        protected void btnReceive_Click(object sender, EventArgs e)
        {
            this.div_Send.Visible = false;
            this.div_Receive.Visible = true;
            this.div_Recycle.Visible = false;
            this.div_Draft.Visible = false;
        }
        //点击草稿纸信箱按钮，隐藏其他GridView
        protected void btnDraft_Click(object sender, EventArgs e)
        {
            this.div_Send.Visible = false;
            this.div_Receive.Visible = false;
            this.div_Recycle.Visible = false;
            this.div_Draft.Visible = true;
        }
        //绑定收信箱数据
        private void dataBindReceiveMessage(IList<MyOA.Model.Message> messageList)
        {
            this.gvReceive.DataSourceID = null;
            this.gvReceive.DataSource = messageList;
            this.gvReceive.DataBind();
        }
        //绑定发信箱数据
        private void dataBindSendMessage(IList<MyOA.Model.Message> messageList)
        {
            this.gvSend.DataSourceID = null;
            this.gvSend.DataSource = messageList;
            this.gvSend.DataBind();
        }
        //绑定删除信息数据
        private void dataBindRecycleMessage(IList<MyOA.Model.Message> messageList)
        {
            this.gvRecycle.DataSourceID = null;
            this.gvRecycle.DataSource = messageList;
            this.gvRecycle.DataBind();
        }
        //绑定草稿信息数据
        private void dataBindDraftMessage(IList<MyOA.Model.Message> messageList)
        {
            this.gvDraft.DataSourceID = null;
            this.gvDraft.DataSource = messageList;
            this.gvDraft.DataBind();
        }
        //保存收信箱页数
        private int pageReveive
        {
            set { ViewState["pageReveive"] = value; }
            get { return (int)ViewState["pageReveive"]; }
        }
        //保存发信箱页数
        private int pageSend
        {
            set { ViewState["pageSend"] = value; }
            get { return (int)ViewState["pageSend"]; }
        }
        //草稿纸页数
        private int pageDraft
        {
            set { ViewState["pageDraft"] = value; }
            get { return (int)ViewState["pageDraft"]; }
        }
        //删除信箱页数
        private int pageRecycle
        {
            set { ViewState["pageRecycle"] = value; }
            get { return (int)ViewState["pageRecycle"]; }
        }
        //收信箱中分页事件
        protected void anpReceive_PageChanged(object sender, EventArgs e)
        {
            int sum;
            string loginId = Session["loginId"].ToString();
            UserInfo userInfo = userInfoManager.GetUserInfoByLoginId(loginId);
            pageReveive = anpReceive.CurrentPageIndex;
            IList<MyOA.Model.Message> messageList = messageManager.GetAllReceiveMessageByUserId(userInfo.UserId, 5, pageReveive, 1, out sum);
            this.dataBindReceiveMessage(messageList);
        }
        //发信箱中分页事件
        protected void anpSend_PageChanged(object sender, EventArgs e)
        {
            int sum;
            string loginId = Session["loginId"].ToString();
            UserInfo userInfo = userInfoManager.GetUserInfoByLoginId(loginId);
            pageSend = anpSend.CurrentPageIndex;
            IList<MyOA.Model.Message> messageList = messageManager.GetAllSendMessageByUserId(userInfo.UserId, 5, pageSend, 1, out sum);
            this.dataBindSendMessage(messageList);
        }
        //已删除信箱分页事件
        protected void anpRecycle_PageChanged(object sender, EventArgs e)
        {
            int sum;
            string loginId = Session["loginId"].ToString();
            UserInfo userInfo = userInfoManager.GetUserInfoByLoginId(loginId);
            pageRecycle = anpRecycle.CurrentPageIndex;
            IList<MyOA.Model.Message> messageList = messageManager.GetAllRecycleMessageByUserId(userInfo.UserId, 5, pageRecycle, 1, out sum);
            this.dataBindRecycleMessage(messageList);

        }
        //草稿纸分页事件
        protected void anpDraft_PageChanged(object sender, EventArgs e)
        {
            int sum;
            string loginId = Session["loginId"].ToString();
            UserInfo userInfo = userInfoManager.GetUserInfoByLoginId(loginId);
            pageDraft = anpDraft.CurrentPageIndex;
            IList<MyOA.Model.Message> messageList = messageManager.GetAllDraftMessageByUserId(userInfo.UserId, 5, pageDraft, 1, out sum);
            this.dataBindRecycleMessage(messageList);
        }

        protected void gvReceive_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string commandName = e.CommandName;
            int messageId = Convert.ToInt32(e.CommandArgument);
            //如果点击详细按钮
            if (commandName == "ReveiceEdit")
            {
                string url = string.Format("BrowseMessage.aspx?messageId={0}&IfRead=1", messageId, 1);
                Response.Redirect(url);
            }
            //如果点击删除按钮
            else if (commandName == "ReveiceDel")
            {
                //根据UserId、messageId得到要修改的MessageTouser
                MessageToUser messageToUser = messageToUserManager.getMessageToUserByMessageIdAndToUserId(userInfo.UserId, messageId);
                messageToUser.IfRecycle = 1;    //修改MessageToUser的IfRecycle属性将其放进删除信箱
                int result = messageToUserManager.updateMessageToUserByMessageIdAndToUserId(messageToUser);
                if (result > 0)
                {
                    word = "短信删除成功";
                }
                else
                {
                    word = "短信删除失败";
                }
            }
            string script = string.Format("<script>Alert('{0}',null,null,'温馨提示','href()')</script>", word);
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", script, false);
            dataBindReceiveMessage();  //重新绑定收信箱
        }

        protected void gvReceive_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=color");
                ImageButton imgbtnDelete = (ImageButton)e.Row.FindControl("imgbtnReveiceDel");
                imgbtnDelete.Attributes.Add("onclick", "return confirm('确定放进回收站吗?')");
            }
        }

        protected void gvSend_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string commandName = e.CommandName;
            int messageId = Convert.ToInt32(e.CommandArgument);
            //如果点击修改按钮
            if (commandName == "sendEdit")
            {
                Response.Redirect("BrowseMessage.aspx?messageId=" + messageId);
            }
            //如果点击删除按钮
            else if (commandName == "sendDel")
            {
                int result = messageToUserManager.deleteMessageToUserByMessageId(messageId);
                int result2 = messageManager.deleteMessageByMessageId(messageId);
                if (result > 0 && result2 > 0)
                {
                    word = "短信删除成功";
                }
                else
                {
                    word = "短信删除失败";
                }
            }
            string script = string.Format("<script>Alert('{0}',null,null,'温馨提示','href()')</script>", word);
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", script, false);
            dataBindSendMessage();        //重新绑定信息
        }
        protected void gvSend_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=color");
                ImageButton imgbtnDel = (ImageButton)e.Row.FindControl("imgbtnSendDel");
                imgbtnDel.Attributes.Add("onclick", "return conform('确定删除吗')");
            }
        }

        protected void gvDraft_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string commandName = e.CommandName;
            int messageId = Convert.ToInt32(e.CommandArgument);
            //如果点击修改按钮
            if (commandName == "draftEdit")
            {
                string url = string.Format("BrowseMessage.aspx?messageId={0}&IfDarft={1}", messageId, 1);
                //用IfDraft标示是否为草稿纸;
                Response.Redirect(url);
            }
            //如果点击删除按钮
            else if (commandName == "draftDel")
            {
                int result = messageToUserManager.deleteMessageToUserByMessageId(messageId);
                int result2 = messageManager.deleteMessageByMessageId(messageId);
                if (result > 0 && result2 > 0)
                {
                    word = "短信删除成功";
                }
                else
                {
                    word = "短信删除失败";
                }
            }
            string script = string.Format("<script>Alert('{0}',null,null,'温馨提示'，'href()')</script>", word);
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", script, false);
            dataBindDraftMessage();
        }
        protected void gvDraft_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroudColor='#6699ff'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=color");
                ImageButton imgbtnDraft = (ImageButton)e.Row.FindControl("imgbtnDraftDel");
                imgbtnDraft.Attributes.Add("onclick", "return conform('确定删除吗')");
            }
        }

        protected void gvRecycle_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string commandName = e.CommandName;
            int messageId = Convert.ToInt32(e.CommandArgument);
            //如果点击修改按钮
            if (commandName == "recycleCancel")
            {
                MessageToUser messageToUser = messageToUserManager.getMessageToUserByMessageIdAndToUserId(userInfo.UserId, messageId);
                messageToUser.IfRecycle = 0;
                int result = messageToUserManager.updateMessageToUserByMessageIdAndToUserId(messageToUser);
                if (result > 0)
                {
                    word = "短信还原成功";
                }
                else
                {
                    word = "短信还原失败";
                }
            }
            //如果点击删除按钮
            else if (commandName == "recycleDel")
            {
                int result = messageToUserManager.deleteMessageToUserByMessageId(messageId);
                int result2 = messageManager.deleteMessageByMessageId(messageId);
                if (result > 0 && result2 > 0)
                {
                    word = "短信删除成功";
                }
                else
                {
                    word = "短信删除失败";
                }
            }
            string script = string.Format("<script>Alert('{0}',null,null,'温馨提示','href()')</script>", word);
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", script, false);
            dataBindRecycleMessage();
        }
        protected void gvRecycle_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
                ImageButton imgbtn = (ImageButton)e.Row.FindControl("imgbtnRecycleDel");
                imgbtn.Attributes.Add("onclick", "ConfirmInfo('您要彻底删除该邮件吗？');");
            }
        }

    }
}
